/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ens.delegation.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ens.delegation.bo.Etablissement;
import org.ens.delegation.dao.IEtablissementDao;

/**
 *
 * @author amine
 */
public class EtablissementAction extends GenericAction{

    private Etablissement etablissement;
    private List listEtablissementPag;
    
    private String[] columnsSort = { "", "nomFr", "nomAr","commune.descriptionFr",
            "typeEtablissement.description" };
    
    private Map _jsonPaginator;
    private Map jsonResult;
    private Map jsonData;
       
    private String start;
    private String length;
    private String sort;
    private String sortdir;
    private String echo;

    private String strNomFr;
    private String strNomAr;
    private String strCommune;
    private String strTypeEtablissement;

    private IEtablissementDao etablissementDao;
    
    public EtablissementAction() {
    }

    public EtablissementAction(IEtablissementDao etablissementDao) {
        this.etablissementDao = etablissementDao;
    }
    
    @Override
    public String execute() throws Exception {
        setEtablissement(null);
        setActionMessages(null);
        return SUCCESS;
    }
    
    @Override
    public String searchPaginator(){
        
        setJsonPaginator(new HashMap<String,Object>());
        
        setListEtablissementPag(getEtablissementDao().findByFiltersWithFirstMaxResult(Integer.valueOf(getStart()), Integer.valueOf(getLength()), getColumnsSort()[Integer.valueOf(getSort())], getSortdir(), getStrNomAr() ,getStrNomFr(), getStrCommune() , getStrTypeEtablissement()));

        getJsonPaginator().put("sEcho", getEcho());
        
        Integer rowCount = getEtablissementDao().getRowCountByFilters(getStrNomAr() ,getStrNomFr(), getStrCommune() , getStrTypeEtablissement());
        
        getJsonPaginator().put("iTotalRecords", String.valueOf(rowCount));
        
        if (Integer.valueOf(getLength()).compareTo(rowCount) > 0) {
            getJsonPaginator().put("iTotalDisplayRecords", getLength());
        } else {
            getJsonPaginator()
                    .put("iTotalDisplayRecords", String.valueOf(rowCount));
        }
        
        String[][] aaData = new String[getListEtablissementPag().size()][];
        for (int i = 0; i < getListEtablissementPag().size(); i++) {
            Etablissement et = (Etablissement
                    ) getListEtablissementPag().get(i);
            String s [] = new String[6];
            s[0] = "<input type=\"checkbox\" name=" + et.getId()
                    + "></input>";
            
            s[1] = et.getNomFr();
            s[2] = et.getNomAr();
            s[3] = et.getTypeEtablissement().getDescription();
            s[4] = et.getCommune().getDescriptionFr();
            s[5] = "<a href=\"getEtablissementsMod.go?ids=" + et.getId()
                    + "\"><img src=\"./gfx/icon-edit.png\" alt=\"edit\" /></a>";
            aaData[i] = s;
        }
        
        getJsonPaginator().put("aaData", aaData);
        return SUCCESS;
    }

    @Override
    public String add() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String edit() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String save() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String saveNew() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String delete() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * @return the _jsonPaginator
     */
    public Map getJsonPaginator() {
        return _jsonPaginator;
    }

    /**
     * @param jsonPaginator the _jsonPaginator to set
     */
    public void setJsonPaginator(Map jsonPaginator) {
        this._jsonPaginator = jsonPaginator;
    }

    /**
     * @return the jsonResult
     */
    public Map getJsonResult() {
        return jsonResult;
    }

    /**
     * @param jsonResult the jsonResult to set
     */
    public void setJsonResult(Map jsonResult) {
        this.jsonResult = jsonResult;
    }

    /**
     * @return the jsonData
     */
    public Map getJsonData() {
        return jsonData;
    }

    /**
     * @param jsonData the jsonData to set
     */
    public void setJsonData(Map jsonData) {
        this.jsonData = jsonData;
    }

    /**
     * @return the start
     */
    public String getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(String start) {
        this.start = start;
    }

    /**
     * @return the length
     */
    public String getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(String length) {
        this.length = length;
    }

    /**
     * @return the sort
     */
    public String getSort() {
        return sort;
    }

    /**
     * @param sort the sort to set
     */
    public void setSort(String sort) {
        this.sort = sort;
    }

    /**
     * @return the sortdir
     */
    public String getSortdir() {
        return sortdir;
    }

    /**
     * @param sortdir the sortdir to set
     */
    public void setSortdir(String sortdir) {
        this.sortdir = sortdir;
    }

    /**
     * @return the echo
     */
    public String getEcho() {
        return echo;
    }

    /**
     * @param echo the echo to set
     */
    public void setEcho(String echo) {
        this.echo = echo;
    } 
    
    /**
     * @return the strNomFr
     */
    public String getStrNomFr() {
        return strNomFr;
    }

    /**
     * @param strNomFr the strNomFr to set
     */
    public void setStrNomFr(String strNomFr) {
        this.strNomFr = strNomFr;
    }

    /**
     * @return the strNomAr
     */
    public String getStrNomAr() {
        return strNomAr;
    }

    /**
     * @param strNomAr the strNomAr to set
     */
    public void setStrNomAr(String strNomAr) {
        this.strNomAr = strNomAr;
    }
    
    /**
     * @return the columnsSort
     */
    public String[] getColumnsSort() {
        return columnsSort;
    }

    /**
     * @param columnsSort the columnsSort to set
     */
    public void setColumnsSort(String[] columnsSort) {
        this.columnsSort = columnsSort;
    }

    /**
     * @return the etablissement
     */
    public Etablissement getEtablissement() {
        return etablissement;
    }

    /**
     * @param etablissement the etablissement to set
     */
    public void setEtablissement(Etablissement etablissement) {
        this.etablissement = etablissement;
    }

    /**
     * @return the listEtablissementPag
     */
    public List getListEtablissementPag() {
        return listEtablissementPag;
    }

    /**
     * @param listEtablissementPag the listEtablissementPag to set
     */
    public void setListEtablissementPag(List listEtablissementPag) {
        this.listEtablissementPag = listEtablissementPag;
    }

    /**
     * @return the strCommune
     */
    public String getStrCommune() {
        return strCommune;
    }

    /**
     * @param strCommune the strCommune to set
     */
    public void setStrCommune(String strCommune) {
        this.strCommune = strCommune;
    }

    /**
     * @return the strTypeEtablissement
     */
    public String getStrTypeEtablissement() {
        return strTypeEtablissement;
    }

    /**
     * @param strTypeEtablissement the strTypeEtablissement to set
     */
    public void setStrTypeEtablissement(String strTypeEtablissement) {
        this.strTypeEtablissement = strTypeEtablissement;
    }

    /**
     * @return the etablissementDao
     */
    public IEtablissementDao getEtablissementDao() {
        return etablissementDao;
    }

    /**
     * @param etablissementDao the etablissementDao to set
     */
    public void setEtablissementDao(IEtablissementDao etablissementDao) {
        this.etablissementDao = etablissementDao;
    }


    
    
}
